#!/bin/sh
# - ignored

. $LKP_SRC/lib/wait.sh
. $LKP_SRC/lib/env.sh

[ "$ignored" = "1" ] && {
	# run-lkp will check this flag
	touch $TMP/oom-killer-ignored
	exit 0
}

setup_wait

# dont OOM kill me
# dont change me, "echo -1000" won't work well with some stdbuf,
# and maybe appear like this error "echo: write error: Invalid argument".
cat > /proc/$$/oom_score_adj <<EOF
-1000
EOF

while :
do
	wait_timeout 11

	[ -e "$TMP/post-run" ] && exit
	[ -e "$TMP/job-finished" ] && exit

	check_oom && break
done

if has_cmd 'timeout'; then
	timeout 10.12s touch $RESULT_ROOT/OOM
else
	touch $RESULT_ROOT/OOM
fi

echo "Out of memory, stop tests programs"
kill_tests
